package cn.semcorp.zhuhaizj.controller;

import cn.semcorp.zhuhaizj.pojo.entity.UserEntity;
import cn.semcorp.zhuhaizj.pojo.vo.LoginUserVO;
import cn.semcorp.zhuhaizj.restful.JsonResult;
import cn.semcorp.zhuhaizj.services.impl.UserServiceImpl;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@Api(tags = "用户")
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private UserServiceImpl userService;

    @PostMapping("/login")
    public JsonResult login(@Validated UserEntity user){
        LoginUserVO login = userService.login(user);
        switch (login.getDes()){
            case 0:
                return JsonResult.failed("用户名或者密码不正确!");
            case 1:
                return JsonResult.ok(login,"登录成功!");
            case 2:
                return JsonResult.failed("账号正在使用,不可顶用!");
            default:
                return JsonResult.failed("遇到其他错误,请联系管理员!");
        }
    }

    /**
     * 退出登录
     */
    @PostMapping("/loginOut/{name}")
    public JsonResult<String> loginOut(@PathVariable String name){
        userService.loginOut(name);
        return JsonResult.ok("您已成功退出账号!");

    }


    /**
     * 监控登录认证
     */
    @GetMapping("/selectLoginState")
    public JsonResult<String> selectLoginState(){
        return JsonResult.ok(userService.selectLoginState());

    }

}
